﻿using Stardust.Common;
using Stardust.Common.Model;
using Stardust.ConfigCenterWeb.Code.Entities;
using Stardust.ConfigCenterWeb.Code.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Stardust.ConfigCenterWeb.Controllers
{
    public class StardustConfigCenterController : Controller
    {
        /// <summary>
        /// 服务节点注册
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public ActionResult Register(ServerNode node)
        {
            node = ServerNodeService.Instance.Register(node);
            var mr = new MessageResult<ServerNode>();
            if (node == null)
            {
                mr.Message = "注册失败";
            }
            else
            {
                mr.Success = true;
                mr.Data = node;
            }
            return Json(mr, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 服务节点心跳
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public ActionResult Heartbeat(long nodeId)
        {
            ServerNodeService.Instance.Heartbeat(nodeId);
            return new EmptyResult();
        }

        /// <summary>
        /// 服务节点断开
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public ActionResult Logout(LogoutParams ps)
        {
            ServerNodeService.Instance.Logout(ps.NodeId, ps.ServiceName);
            return new EmptyResult();
        }

        /// <summary>
        /// 客户端获取服务节点
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public ActionResult GetNodesByName(string serviceName)
        {
            var nodes = ServerNodeService.Instance.GetNodesByName(serviceName);
            var maxEventId = NodeEventService.Instance.GetMaxId();
            var dto = new NodeGroupDTO
            {
                Nodes = nodes.Select(x => new Common.Model.ServerNodeModel
                {
                    Id = x.Id,
                    Address = x.Address,
                    ServiceName = x.ServiceName,
                    Status = x.Status,
                    Weight = x.Weight,
                    Version = x.Version,
                    HeartbeatTime = x.HeartbeatTime
                }).ToList(),
                MaxEventId = maxEventId
            };
            return Json(new { nodes, maxEventId }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 客户端获取调用服务更新
        /// </summary>
        /// <param name="serviceNames"></param>
        /// <returns></returns>
        public ActionResult GetNodesUpdates(List<GetNodesUpdateParams> ps)
        {
            var eventList = NodeEventService.Instance.GetNodesUpdates(ps);
            var maxEventId = NodeEventService.Instance.GetMaxId();
            var result = new GetNodesUpdateResult
            {
                EventList = eventList.Select(x => new NodeEventModel
                {
                    EventType = x.EventType,
                    Id = x.Id,
                    ServerNode = new ServerNodeModel
                    {
                        Id = x.ServerNode.Id,
                        Address = x.ServerNode.Address,
                        HeartbeatTime = x.ServerNode.HeartbeatTime,
                        ServiceName = x.ServerNode.ServiceName,
                        Status = x.ServerNode.Status,
                        Version = x.ServerNode.Version,
                        Weight = x.ServerNode.Weight
                    },
                    ServerNodeId = x.ServerNodeId,
                    TriggerTime = x.TriggerTime,
                }).ToList(),
                MaxEventId = maxEventId,
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}